﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinFu.Reflection;
using Hiro.ActivationPoints;
using Hiro.ActivationPoints.Interfaces;

namespace Hiro.Loaders
{
    /// <summary>
    /// A class that loads all types within a given assembly as activation points.
    /// </summary>
    internal class TypeActivationPointLoader :
        IActionLoader<IList<ITypeActivationPoint>, Type>
    {
        private readonly IArgumentResolver _argumentResolver;
        private readonly IPropertyResolver _propertyResolver;

        public TypeActivationPointLoader(IArgumentResolver argumentResolver, 
            IPropertyResolver propertyResolver)
        {
            _argumentResolver = argumentResolver;
            _propertyResolver = propertyResolver;
        }

        public bool CanLoad(Type inputType)
        {
            return inputType.IsClass && !inputType.IsInterface &&
                   !inputType.IsAbstract && inputType.IsPublic;
        }

        public IEnumerable<Action<IList<ITypeActivationPoint>>> Load(Type input)
        {
            yield return list =>
                         list.Add(input.ToActivationPoint(_argumentResolver,
                                                          _propertyResolver));
        }
    }
}
